//
// Created by WenDaojiang on 15/11/21.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import "HybridSocialAction.h"
//

@implementation HybridSocialAction {

}

+ (void)initialize {
    [super initialize];
    [HybridSocialInitialize instance];
}

/**
 * 显示分享菜单
 */
- (void)showShareActionSheet:(id)params {

    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKEnableUseClientShare];

    NSString *text = params[@"text"];
    NSString *title = params[@"title"];
    NSString *image = params[@"image"];
    NSURL *url = [NSURL URLWithString:params[@"url"]];
    SSDKContentType contentType  = (SSDKContentType) [params[@"contentType"] integerValue];

    [shareParams SSDKSetupShareParamsByText: text
                                     images: image
                                        url: url
                                      title: title
                                       type: contentType];

    // 设置新浪微博分享内容
    [shareParams SSDKSetupSinaWeiboShareParamsByText:[text stringByAppendingFormat:@" %@", url.absoluteString]
                                               title:title
                                               image:image
                                                 url:url latitude:0.0F longitude:0.0F
                                            objectID:nil
                                                type:contentType];

    // 设置腾讯微博分享内容
    [shareParams SSDKSetupTencentWeiboShareParamsByText:text
                                                 images:image
                                               latitude:0.0F
                                              longitude:0.0F
                                                   type:contentType];


    [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                             items:params[@"platforms"]
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

                   NSDictionary *resultData = @{
                           @"state" : @(state),
                           @"platform" : @(platformType),
                           @"error": [HybridSocialInitialize Nil2NSNull:error.userInfo[@"user_data"]]
                   };

                   NSLog(@"error %@", error);

                   switch (state) {
                       case SSDKResponseStateSuccess: {
                           [self callback:YES data:resultData];
                           break;
                       }
                       case SSDKResponseStateFail: {
                           [self callback:NO data:resultData];
                           break;
                       }
                       case SSDKResponseStateCancel: {
                           [self callback:NO data:resultData];
                           break;
                       }
                       default:
                           //[self callback:NO data:error.userInfo];
                           break;
                   }

               }
    ];
}

/**
 * 分享
 * @param params[@"platform"]  平台类型
 */
-(void)share:(id)params{

    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:params[@"text"]
                                     images:params[@"image"]
                                        url:[NSURL URLWithString:params[@"url"]]
                                      title:params[@"title"]
                                       type:(SSDKContentType) [params[@"contentType"] integerValue]];
    [ShareSDK share:platformType
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {

        NSDictionary *resultData = @{
                @"state" : @(state),
                @"platform" : @(platformType)
        };

        switch (state) {
            case SSDKResponseStateSuccess: {
                [self callback:YES data:resultData];
                break;
            }
            case SSDKResponseStateFail: {
                [self callback:NO data:resultData];
                break;
            }
            case SSDKResponseStateCancel: {
                [self callback:NO data:resultData];
                break;
            }
            default:
                break;
        }
    }];
}

/**
 * authorize
 */
- (void)authorize:(id)params{

    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];

    [ShareSDK getUserInfo: platformType
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
               if (state == SSDKResponseStateSuccess) {

                   NSLog(@"uid = %@", user.uid);
                   NSLog(@"credential %@", user.credential);
                   NSLog(@"token = %@", user.credential.token);
                   NSLog(@"nickname = %@", user.nickname);

                   [self callback:YES data: [HybridSocialInitialize User2Dictionary:user]];
               }else {
                   NSLog(@"%@", error);
                   [self callback:NO data:error == nil ? @"unkown error." : error.userInfo];
               }

           }];
}

/**
 *  判断分享平台是否授权
 *
 *  @param params[@"platform"]  平台类型
 */
- (void)hasAuthorized:(id)params {
    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];
    [self callback:[ShareSDK hasAuthorized:platformType] data:nil];
}


/**
 *  取消分享平台授权
 *
 *  @param params[@"platform"]  平台类型
 */
- (void)cancelAuthorize:(id)params {
    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];
    [ShareSDK cancelAuthorize:platformType];
    [self callback:YES data:nil];
}

/**
 * 获取好友列表
 *  @param params.platformType       平台类型
 *  @param params.page             分页游标
 *  @param params.pageSize               分页大小
 */
- (void)getFriends:(id)params{

    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];
    NSUInteger page = [params[@"page"] unsignedIntegerValue];
    NSUInteger pageSize = [params[@"pageSize"] unsignedIntegerValue];
    [ShareSDK getFriends:platformType
                  cursor:page
                    size:pageSize
          onStateChanged:^(SSDKResponseState state, SSDKFriendsPaging *paging, NSError *error) {
              if (state == SSDKResponseStateSuccess){

                  NSMutableArray *users = [NSMutableArray arrayWithCapacity:paging.users.count];

                  for (int i = 0; i < paging.users.count; ++i) {
                      [users addObject:[HybridSocialInitialize User2Dictionary:paging.users[i]]];
                  }

                  [self callback:YES data:@{
                          @"hasNext": @(paging.hasNext),
                          @"nextCursor": @(paging.nextCursor),
                          @"prevCursor": @(paging.prevCursor),
                          @"total": @(paging.total),
                          @"users": users,
                  }];

              }else{
                  [self callback:NO data: error == nil ? nil : error.userInfo];
              }
    }];
    //NSLog(@"----- getFriends -----");
}

/**
 * 获取用户信息
 */
- (void)getUserInfo:(id)params{

    SSDKPlatformType platformType = (SSDKPlatformType) [params[@"platform"] integerValue];
    SSDKUserQueryConditional *conditional = nil;
    if(params[@"uid"]){
        conditional = [SSDKUserQueryConditional userQueryConditionalByUserId:params[@"uid"]];
    }else if(params[@"userName"]){
        conditional = [SSDKUserQueryConditional userQueryConditionalByUserName:params[@"userName"]];
    }else{
        [self callback:NO data:@"params error."];
        return;
    }
    [ShareSDK getUserInfo:platformType
              conditional:conditional
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
               if (state == SSDKResponseStateSuccess){
                   NSLog(@"user: %@", user);
                   [self callback:YES data: [HybridSocialInitialize User2Dictionary:user]];
               }else{
                   [self callback:NO data: error == nil ? nil : error.userInfo];
               }
    }];
}

@end